package com.blog.check.check;

import com.blog.check.resultenum.BlogChkRsEnum;
import com.blog.dao.domain.BlogDO;
import com.blog.dao.domain.BlogTypeDO;
import com.blog.form.BlogForm;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 博客检查
 *
 * @author 泡面ing
 * @date 2021/12/13
 */
@Service
public class BlogCheck extends BaseCheck{


    /**
     * check for get
     *
     */
    public CheckResult checkGet(String blogId){
        // Non-logical check for update
        return checkGetNonLogic(blogId);
    }

    private CheckResult checkGetNonLogic(String blogId) {
        BlogDO blogDO = blogDAO.get(blogId);
        if (ObjectUtils.isEmpty(blogDO)){
            return CheckResult.failure(BlogChkRsEnum.ILLEGAL_ID_NOT_EXIST,blogId);
        }
        return CheckResult.success();
    }

    /**
     * check for save
     *
     */
    public CheckResult checkSave(BlogForm blogForm){
        // Non-logical check for save
        CheckResult checkResult = checkSaveNonLogic(blogForm);
        if (checkResult.getCode() != CheckResult.success().getCode()){
            return checkResult;
        }
        // logical check for save
        checkResult = checkSaveLogic(blogForm);
        return  checkResult;
    }

    private CheckResult checkSaveLogic(BlogForm blogForm) {
        if(StringUtils.isBlank(blogForm.getTitle())){
            return CheckResult.failure(BlogChkRsEnum.TITLE_MUST_NOT_BE_NULL);
        }
        if(StringUtils.isBlank(blogForm.getContent())){
            return CheckResult.failure(BlogChkRsEnum.CONTENT_MUST_NOT_NULL);
        }
        if (StringUtils.isBlank(blogForm.getTypeId())){
            return CheckResult.failure(BlogChkRsEnum.TITLE_MUST_NOT_BE_NULL);
        }
        return CheckResult.success();
    }

    private CheckResult checkSaveNonLogic(BlogForm blogForm) {
        BlogTypeDO blogTypeDO = blogTypeDAO.get(blogForm.getTypeId());
        if (ObjectUtils.isEmpty(blogTypeDO)){
            return CheckResult.failure(BlogChkRsEnum.ILLEGAL_TYPE_NOT_EXIST,blogForm.getTypeId());
        }
        if (getUsername() == null){
            return CheckResult.failure(BlogChkRsEnum.USER_IS_NOT_LOGIN);
        }
        return CheckResult.success();
    }

    /**
     * check for update
     *
     */
    public CheckResult checkUpdate(String blogId){
        // Non-logical check for update
        CheckResult checkResult = checkUpdateNonLogic(blogId);
        if (checkResult.getCode() != CheckResult.success().getCode()){
            return checkResult;
        }
        // login check for update
        checkResult = login(blogId);
        return checkResult;
    }


    private CheckResult checkUpdateNonLogic(String blogId) {
        BlogDO blogDO = blogDAO.get(blogId);
        if (ObjectUtils.isEmpty(blogDO)){
            return CheckResult.failure(BlogChkRsEnum.ILLEGAL_ID_NOT_EXIST,blogId);
        }
        return CheckResult.success();
    }
    /**
     * check for remove
     *
     */
    public CheckResult checkRemove(String blogId){
        // Non-logical check for remove
        CheckResult checkResult = checkRemoveNonLogic(blogId);
        if (checkResult.getCode() != CheckResult.success().getCode()){
            return checkResult;
        }
        // login check for remove
        checkResult = login(blogId);
        return checkResult;
    }

    private CheckResult login(String blogId) {

        BlogDO blogDO = blogDAO.get(blogId);

        if (!Objects.equals(getUsername(), blogDO.getKeyWord())){
            return CheckResult.failure(BlogChkRsEnum.USER_IS_NOT_LOGIN);
        }
        return CheckResult.success();
    }

    private CheckResult checkRemoveNonLogic(String blogId) {
        BlogDO blogDO = blogDAO.get(blogId);
        if (ObjectUtils.isEmpty(blogDO)){
            return CheckResult.failure(BlogChkRsEnum.ILLEGAL_ID_NOT_EXIST,blogId);
        }
        return CheckResult.success();
    }

}
